package com.putao.album.child;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.putao.album.R;
import com.putao.album.base.BaseActivity;
import com.putao.album.util.ActivityHelper;
import com.putao.album.util.CommonUtils;
import com.putao.album.util.HttpRequestUtil;
import com.putao.album.util.Loger;
import com.putao.album.util.PuTaoConstants;
import com.putao.album.util.StringHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by jidongdong on 15/6/10.
 */
public class ActivityQRcode extends BaseActivity {

    private ImageView image_qr_code;
    private TextView desc_tv, how_to_scan_code;
    private String relation;

    @Override
    public int doGetContentViewId() {
        return R.layout.activity_qr_code;
    }

    @Override
    public void doInitSubViews(View view) {
        image_qr_code = queryViewById(R.id.image_qr_code);
        Button back_btn = queryViewById(R.id.back_btn, true);
        back_btn.setBackgroundResource(R.drawable.all_button_close);
        how_to_scan_code = queryViewById(R.id.how_to_scan_code, true);
        how_to_scan_code.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        how_to_scan_code.getPaint().setAntiAlias(true);
        queryViewById(R.id.right_btn_ll).setVisibility(View.GONE);
        ((TextView) queryViewById(R.id.title_tv)).setText("二维码");
        desc_tv = queryViewById(R.id.desc_tv);
        desc_tv.setText("成为\"" + CommonUtils.getBabyName() + "\"的家人");
    }

    @Override
    public void doInitData() {
        if (getIntent() != null) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                relation = bundle.getString("relation");
            }
        }

        getQRcode();
    }

    void getQRcode() {
        Loger.d("relation:" + relation);
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", CommonUtils.getUsrId());
        params.put("relationship", StringHelper.isEmpty(relation) ? "亲友关系" : relation);
        params.put("baby_id", CommonUtils.getBabyId());
        params.put("type", "3");
        HttpRequestUtil.doHttpPostRequest(mActivity, PuTaoConstants.API_SEND_INVITATION,
                new RequestParams(HttpRequestUtil.buildHttpRequestParams(params)), new HttpRequestUtil.RequestResponseListener() {
                    @Override
                    public void success(int statuscode, String response) {
                        if (!StringHelper.isEmpty(response)) {
                            try {
                                JSONObject object = new JSONObject(response);
                                String qrcodeurl = object.optString("qrcodeUrl");
                                if (!StringHelper.isEmpty(qrcodeurl)) {
                                    ImageLoader.getInstance().displayImage(qrcodeurl, image_qr_code);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void failed(int statuscode, String response) {

                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.how_to_scan_code:
                ActivityHelper.startActivity(mActivity, ActivityHowScanCode.class);
                break;
        }
    }
}
